#include "storagetest.h"

/*Registry this TestSuite to theTestFactoryRegistry*/
CPPUNIT_TEST_SUITE_REGISTRATION (StorageTest);

void StorageTest::setUp() {
	SG_init();
}

void StorageTest::testGetResource() { fprintf(stderr,">>> StorageTest::testGetResource()\n");
	Resource *s = new Storage("sto");
	CPPUNIT_ASSERT(!strcmp("sto",s->getName()));
	CPPUNIT_ASSERT(s->getType()==SG_HOST);	
	
	delete s;
}

void StorageTest::testGetDataElementByName() { fprintf(stderr,">>> StorageTest::testGetDataElementByName()\n");
	Storage *s = new Storage("sto");
	CPPUNIT_ASSERT(s->getNumberOfDatas()==0);	
	s->addData("input_1", 500, DataElement::INPUT_DATA);
	CPPUNIT_ASSERT(s->getNumberOfDatas()==1);	
	s->addData("output_1", 100, DataElement::OUTPUT_DATA);
	try {
		s->getDataElementByName("NOT_EXIST");		
		CPPUNIT_FAIL("An exception should be throwed!!");
	} catch (std::runtime_error &e) {
		//Pass
	}
	
	DataElement *in = s->getDataElementByName("input_1");		
	DataElement *out = s->getDataElementByName("output_1");			
	
	CPPUNIT_ASSERT(!strcmp(in->getName(), "input_1"));
	CPPUNIT_ASSERT(in->getSize()==500);
	CPPUNIT_ASSERT(in->getType()==DataElement::INPUT_DATA);		
	
	CPPUNIT_ASSERT(!strcmp(out->getName(), "output_1"));	
	CPPUNIT_ASSERT(out->getSize()==100);
	CPPUNIT_ASSERT(out->getType()==DataElement::OUTPUT_DATA);		
	
	CPPUNIT_ASSERT(s->getNumberOfDatas()==2);
	
	delete s;
	

}

void StorageTest::testExistDataElement() { fprintf(stderr,">>> StorageTest::testExistDaElement()\n");
	Storage *s = new Storage("sto");

	s->addData("input_1", 500, DataElement::INPUT_DATA);	
	
	DataElement *d1 = s->existDataElement("input_1");
	DataElement *d2 = s->existDataElement("input_2");
	
	CPPUNIT_ASSERT(d1!=NULL);
	CPPUNIT_ASSERT(d1->getSize()==s->getDataElementByName("input_1")->getSize());
	CPPUNIT_ASSERT(d2==NULL);	
}

void StorageTest::tearDown() {
	SG_clear();
}
